<template>
  <div class="container">
    <!-- 导航栏 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="学科名称">
            <el-input v-model="subjectName"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button size="small" @click="eliminateFn">清除</el-button>
            <el-button type="primary" @click="onSubmit" size="small">搜索</el-button>
          </el-form-item>
          <el-button
            type="success"
            style="float: right; !padding: 3px 3px"
            icon="el-icon-edit"
            size="small"
            @click="addFn"
            >新增学科</el-button
          >
        </el-form>
        <!-- <el-button type="info" plain size="medium" style="width:1300px" icon="el-icon-info">
          数据一共十四条
        </el-button> -->
        <el-alert :title="`数据一共${total}条`" type="info" show-icon :closable="false" />
      </div>
      <!-- 表格区域 -->
      <el-table :data="this.query.subjectName" style="width: 100%">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="subjectName" label="学科名称"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column label="创建日期" width="160px">
          <template #default="{row}">
            {{ $dataFormat(row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="isFrontDisplay" label="前台是否显示">
          <template v-slot="scope">
            {{ scope.row.isFrontDisplay == 1 ? '是' : '否' }}
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录"> </el-table-column>
        <el-table-column prop="tags" label="标签"> </el-table-column>
        <el-table-column prop="totals" label="题目数量"> </el-table-column>
        <el-table-column label="操作" width="280">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="classificationFn(scope.row.id)">学科分类</el-button>
            <el-button type="text" size="small" @click="labelFn(scope.row.id)">学科标签</el-button>
            <el-button type="text" size="small" @click="updateFn(scope.row)">修改</el-button>
            <el-button type="text" size="small" @click="deleteFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页屈区域 -->
      <div class="block">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.page"
          :page-size="query.pagesize"
          :page-sizes="[10, 15, 20, 25]"
          layout=" sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      <!-- 弹窗 -->
      <el-dialog title="新增学科" :visible.sync="showDialog" width="400px" :show-close="false">
        <SubJects @close="showDialog" ref="addForm" @Dlogs="btnFn" @enterEv="addFnn" :xiugai="isEdit"></SubJects>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import { list, add, remove } from '@/api/hmmm/subjects.js'
import SubJects from '@/module-hmmm/components/subjects-add.vue'
export default {
  // 弹框组件
  components: {
    SubJects
  },
  data () {
    return {
      formInline: {
        user: '',
        region: ''
      },
      // 页码
      query: {
        page: 1, // 当前页
        pagesize: 10, // 页面显示的条数
        subjectName: []
      },
      subjectName: '',
      // list: [],
      total: 0, // 数据条数
      // 新增学科的弹窗隐藏
      showDialog: false,
      isEdit: false // 修改状态为true 新增状态为false
    }
  },
  created () {
    this.subjectAPI()
  },
  methods: {
    // 点击清除按钮
    eliminateFn () {
      this.subjectName = ''
    },

    // 成员信息列表
    async subjectAPI () {
      const res = await list(this.query)
      // console.log(res)
      this.query.subjectName = res.data.items
      this.total = res.data.counts
    },
    // 分页区域
    handleSizeChange (size) {
      this.query.pagesize = size
      this.subjectAPI()
      // console.log(size)
    },
    handleCurrentChange (page) {
      this.query.page = page
      this.subjectAPI()
      // console.log(page)
    },
    // 新增学科弹窗显示
    addFn () {
      this.isEdit = false
      this.showDialog = true
    },
    // 弹窗关闭
    btnFn (Bloo) {
      this.showDialog = Bloo
      this.subjectAPI()
    },
    // 学科添加
    async addFnn (data) {
      this.isEdit = false
      const res = await add(data)
      // console.log(res)
      this.subjectAPI()
    },
    // 点击删除按钮
    deleteFn (data) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(data)
        this.subjectAPI()
      }).catch(() => {

      })
    },
    // 点击修改按钮
    updateFn (id) {
      this.isEdit = true
      this.showDialog = true
      setTimeout(() => {
        this.$refs.addForm.detailFn(id)
      }, 0)
    },
    // 点击搜索按钮
    async onSubmit () {
      const obj = {}
      obj.subjectName = this.subjectName
      obj.page = this.page
      obj.pagesize = this.pagesize
      const res = await list(obj)
      // console.log(res)
      this.query.subjectName = res.data.items
    },
    // 点击学科标签
    labelFn (id) {
      this.$router.push(`/subjects/tags?id=${id}`)
    },
    // 点击分类标签
    classificationFn (id) {
      this.$router.push(`/subjects/directorys?id=${id}`)
    }
  }
}
</script>

<style scoped>
.block {
  text-align: right;
}
</style>
